package handles

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user-reg-mgr/models"
)

func Add(c *gin.Context) {
	name := c.PostForm("name")
	comment := c.PostForm("comment")

	data := models.Video{

		Name:    name,
		Comment: comment,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return
}

func VideoList(c *gin.Context) {

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var video []models.Video

	models.DB.Offset(offset).Limit(size).Find(&video)

	var total int64
	models.DB.Model(&models.Video{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  video,
		"total": total,
	})
}

func VideoDetail(c *gin.Context) {
	id := c.Query("id")
	var video models.Video

	models.DB.Where("id=?", id).Find(&video)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": video,
	})
}
